package com.example.demo.controller

import com.example.demo.config.Constant
import com.example.demo.service.WebService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping


@Controller
class TemplateController @Autowired constructor(
    private val mWebService: WebService
) {

    @RequestMapping("/login")
    fun getLoginView(): String {
        return "febs/views/login"
    }

    @RequestMapping("/index")
    fun getIndexView(model: Model): String {
        model.addAttribute(
            "user",
            "{\"userId\":234,\"username\":\"scott\",\"password\":\"It's a secret\",\"deptId\":3,\"email\":\"scott@gmasdfasil.com\",\"mobile\":\"11011011011\",\"status\":\"1\",\"createTime\":\"2020-03-05T16:31:54.000+08:00\",\"modifyTime\":\"2021-01-05T15:27:57.000+08:00\",\"lastLoginTime\":\"2021\\u5E7401\\u670805\\u65E5 15\\u65F652\\u520611\\u79D2\",\"sex\":\"0\",\"avatar\":\"20180414165920.jpg\",\"theme\":\"black\",\"isTab\":\"1\",\"description\":\"\",\"deptName\":\"\\u5F00\\u53D1\\u4E8C\\u90E8\",\"createTimeFrom\":null,\"createTimeTo\":null,\"roleId\":\"2\",\"roleName\":\"\\u6CE8\\u518C\\u8D26\\u6237\",\"deptIds\":null,\"stringPermissions\":[\"user:view\",\"generator:view\",\"febs:tools:view\",\"dept:add\",\"apex:radar:view\",\"job:export\",\"role:add\",\"online:view\",\"swagger:view\",\"job:log:export\",\"role:export\",\"febs:icons:view\",\"menu:export\",\"loginlog:view\",\"dept:export\",\"others:datapermission\",\"apex:column:view\",\"menu:view\",\"role:view\",\"user:export\",\"generator:generate\",\"apex:mix:view\",\"jvm:view\",\"eximport:template\",\"job:add\",\"apex:area:view\",\"others:eximport:view\",\"dept:view\",\"eximport:import\",\"apex:line:view\",\"map:view\",\"httptrace:view\",\"apex:bar:view\",\"log:view\",\"febs:formgroup:view\",\"job:log:view\",\"menu:add\",\"job:view\",\"generator:configure:view\",\"others:febs:others\",\"loginlog:export\",\"eximport:export\",\"febs:form:view\",\"log:export\",\"server:view\"],\"roles\":[\"\\u6CE8\\u518C\\u8D26\\u6237\"],\"id\":\"scott\"}"
        )
        model.addAttribute("permissions", "\\u6CE8\\u518C\\u8D26\\u6237")
        model.addAttribute(
            "roles",
            "\"user:view\",\"generator:view\",\"febs:tools:view\",\"dept:add\",\"apex:radar:view\",\"job:export\",\"role:add\",\"online:view\",\"swagger:view\",\"job:log:export\",\"role:export\",\"febs:icons:view\",\"menu:export\",\"loginlog:view\",\"dept:export\",\"others:datapermission\",\"apex:column:view\",\"menu:view\",\"role:view\",\"user:export\",\"generator:generate\",\"apex:mix:view\",\"jvm:view\",\"eximport:template\",\"job:add\",\"apex:area:view\",\"others:eximport:view\",\"dept:view\",\"eximport:import\",\"apex:line:view\",\"map:view\",\"httptrace:view\",\"apex:bar:view\",\"log:view\",\"febs:formgroup:view\",\"job:log:view\",\"menu:add\",\"job:view\",\"generator:configure:view\",\"others:febs:others\",\"loginlog:export\",\"eximport:export\",\"febs:form:view\",\"log:export\",\"server:view\""
        )
        return "index"
    }

    @RequestMapping("febs/views//index")
    fun getIndexViews(model: Model): String {
        return "febs/views/index"
    }

    @GetMapping("febs/views/layout")
    fun layout(): String? {
        return "febs/views/layout"
    }

    /**
     * 个人信息
     */
    @GetMapping("febs/views/user/profile")
    fun userProfile(): String? {
        return "febs/views/system/user/userProfile"
    }

    /**
     * 更换头像
     */
    @GetMapping("febs/views/user/avatar")
    fun userAvatar(): String? {
        return "febs/views/system/user/avatar"
    }

    /**
     * 密码修改
     */
    @GetMapping(Constant.VIEW_PREFIX + "password/update")
    fun passwordUpdate(): String? {
        return Constant.VIEW_PREFIX + "system/user/passwordUpdate"
    }

    /**
     * 个人信息修改页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "user/profile/update/{id}")
    fun profileUpdate(@PathVariable id: Int, model: Model): String? {
        val userInfo = mWebService.getUserInfo(id)
        model.addAttribute("user", userInfo)
        resolvePatientInfoModel(id, model, true)
        return Constant.VIEW_PREFIX + "system/user/profileUpdate"
    }

    /**
     * 病床一览表
     */
    @GetMapping(Constant.VIEW_PREFIX + "bedside/bedlist")
    fun systemUser(): String? {
        return Constant.VIEW_PREFIX + "bedside/bedlist"
    }

    /**
     * 护理标签
     */
    @GetMapping(Constant.VIEW_PREFIX + "tagmanage/nursingclass")
    fun tagmanageNursingclass(): String? {
        return Constant.VIEW_PREFIX + "tagmanage/nursingclass"
    }

    /**
     * 护士用户管理
     */
    @GetMapping(Constant.VIEW_PREFIX + "user/nurse")
    fun userNurse(): String? {
        return Constant.VIEW_PREFIX + "user/nurse"
    }

    /**
     * 医生用户管理
     */
    @GetMapping(Constant.VIEW_PREFIX + "user/doctor")
    fun userDoctor(): String? {
        return Constant.VIEW_PREFIX + "user/doctor"
    }

    /**
     * 患者详情页
     */
    @GetMapping(Constant.VIEW_PREFIX + "system/user/detail/{patientId}")
    fun systeserDetail(@PathVariable patientId: Int, model: Model): String {
        val patientInfo = mWebService.getPatientInfo(patientId)
        model.addAttribute("user", patientInfo)
        resolvePatientInfoModel(patientId, model, true)
        return Constant.VIEW_PREFIX + "system/user/userDetail"
    }

    /**
     * 满意度调查表页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "questionn/quest")
    fun getQuestView(): String? {
        return Constant.VIEW_PREFIX + "questionn/quest"
    }

    /**
     * 添加满意度调查表页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "questionn/questAdd")
    fun getAddQuestView(): String? {
        return Constant.VIEW_PREFIX + "questionn/questAdd"
    }

    /**
     * 满意度调查表 详情页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "questionn/questDetail/{id}")
    fun getDetailQuestView(@PathVariable id: Int, model: Model): String? {
        val questionnaireVo = mWebService.getDetailQuestionnaire(id)
        model.addAttribute("data", questionnaireVo)
        resolvePatientInfoModel(id, model, true)
        return Constant.VIEW_PREFIX + "questionn/questDetail"
    }

    /**
     *  添加apk 页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "appapk/addapk")
    fun getAddapkView(model: Model): String? {
        return Constant.VIEW_PREFIX + "appapk/addapk"
    }

    /**
     *  添加菜品 页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "food/addfood")
    fun getAddFoodView(model: Model): String? {
        return Constant.VIEW_PREFIX + "food/addfood"
    }

    /**
     *  菜品类型 页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "food/typefood")
    fun getFoodTypeView(): String? {
        return Constant.VIEW_PREFIX + "food/typefood"
    }

    /**
     *  菜品详情 页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "food/editfood/{id}")
    fun getEditFoodView(@PathVariable id: Int, model: Model): String? {
        val data = mWebService.getFoodInfo(id)
        model.addAttribute("data", data)
        resolvePatientInfoModel(id, model, true)
        return Constant.VIEW_PREFIX + "food/editfood"
    }

    /**
     *  添加商品类型 页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "/mall/typeproduct")
    fun getProductTypeView(): String? {
        return Constant.VIEW_PREFIX + "mall/typeproduct"
    }

    /**
     *  添加商品页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "/mall/addproduct")
    fun addProductView(): String? {
        return Constant.VIEW_PREFIX + "mall/addproduct"
    }

    /**
     *  JVM信息页面
     */
    @GetMapping(Constant.VIEW_PREFIX + "/monitor/jvmInfo")
    fun getJvmView(model: Model): String? {
//        val jvm: List<FebsMetricsEndpoint.FebsMetricResponse> = actuatorHelper.getMetricResponseByType("jvm")
//        val jvmInfo: JvmInfo = actuatorHelper.getJvmInfoFromMetricData(jvm)
//        model.addAttribute("jvm", jvmInfo)
        return Constant.VIEW_PREFIX + "monitor/jvmInfo"
    }

    /**
     * 服务器信息
     */
    @GetMapping(Constant.VIEW_PREFIX + "/monitor/serverInfo")
    fun serverInfo(model: Model): String? {
//        val jdbcInfo: List<FebsMetricsEndpoint.FebsMetricResponse> = actuatorHelper.getMetricResponseByType("jdbc")
//        val systemInfo: List<FebsMetricsEndpoint.FebsMetricResponse> = actuatorHelper.getMetricResponseByType("system")
//        val processInfo: List<FebsMetricsEndpoint.FebsMetricResponse> = actuatorHelper.getMetricResponseByType("process")
//        val serverInfo: ServerInfo = actuatorHelper.getServerInfoFromMetricData(jdbcInfo, systemInfo, processInfo)
//        model.addAttribute("server", serverInfo)
        return Constant.VIEW_PREFIX + "monitor/serverInfo"
    }

    /**
     * tomcat信息
     */
    @GetMapping(Constant.VIEW_PREFIX + "/monitor/tomcatInfo")
    fun tomcatInfo(model: Model): String? {
//        val tomcat: List<FebsMetricsEndpoint.FebsMetricResponse> = actuatorHelper.getMetricResponseByType("tomcat")
//        val tomcatInfo: TomcatInfo = actuatorHelper.getTomcatInfoFromMetricData(tomcat)
//        model.addAttribute("tomcat", tomcatInfo)
        return Constant.VIEW_PREFIX + "monitor/tomcatInfo"
    }

    /**
     * 设备列表
     */
    @GetMapping(Constant.VIEW_PREFIX + "device/listdevice")
    fun getlistdevice(): String? {
        return Constant.VIEW_PREFIX + "device/listdevice"
    }

    /**
     * 宣教视频
     */
    @GetMapping(Constant.VIEW_PREFIX + "video/addvideo")
    fun getAddVideo(): String? {
        return Constant.VIEW_PREFIX + "video/addvideo"
    }

    @GetMapping(Constant.VIEW_PREFIX + "system/user/update/{patientId}")
    fun systemUserUpdate(@PathVariable patientId: Int, model: Model): String? {
        resolvePatientInfoModel(patientId, model, true)
        return Constant.VIEW_PREFIX + "system/user/userUpdate"
    }

    @GetMapping("febs/views/404")
    fun error404(): String? {
        return "error/404"
    }

    fun resolvePatientInfoModel(patientId: Int, model: Model, transform: Boolean) {
        val patientInfo = mWebService.getPatientInfo(patientId)
        model.addAttribute("user", patientInfo)
    }

}